package com.looeyWei;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class MyControl extends JPanel {

    private boolean hOver = false;
    public MyControl(){
//        this.addMouseMotionListener(new MyMouseMotionListener());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 控件的宽高
        int width = this.getWidth();
        int height = this.getHeight();
        // 填空颜色
        g.setColor(Color.RED);
        g.fillRect(0, 0, width, height);

        if(hOver){
            // 绘制边框
            g.setColor(Color.BLUE);
            g.drawRect(0, 0, width - 1, height - 1);
        }else{
            // 绘制边框
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, width - 1, height - 1);
        }
    }

    //
    private class MyMouseMotionListener implements MouseMotionListener {

        @Override
        public void mouseDragged(MouseEvent e) {
            System.out.println("拖动");
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            System.out.println("移动");
        }
    }
}
